﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace YanZhiwei.DotNet2.Utilities.Common
{
    /// <summary>
    /// 资源文件操作帮助类
    /// </summary>
    public class ResourceHelper
    {
        #region 将嵌入的资源写入到本地
        /// <summary>
        /// 将嵌入的资源写入到本地
        /// </summary>
        /// <param name="resourceName">嵌入的资源名称【名称空间.资源名称】</param>
        /// <param name="filename">写入本地的路径</param>
        /// <returns>是否成功</returns>
        public static bool ExtractEmbeddedResourceToFile(string resourceName, string filename)
        {
            bool _result = false;
            try
            {
                Assembly _curCall = Assembly.GetCallingAssembly();
                //string _curCallAssemblyName = _curCall.GetName(true).Name;
                //resourceName = string.Format("{0}.{1}", _curCallAssemblyName, resourceName);
                using (Stream stream = _curCall.GetManifestResourceStream(resourceName))
                {
                    if (stream != null)
                    {
                        using (FileStream fs = new FileStream(filename, FileMode.Create))
                        {
                            byte[] _byte = new byte[stream.Length];
                            stream.Read(_byte, 0, _byte.Length);
                            fs.Write(_byte, 0, _byte.Length);
                            _result = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _result = false;
                Debug.WriteLine(string.Format("ExtractEmbeddedResourceToFile Exception:{0}", ex.Message));
            }
            return _result;
        }
        #endregion
    }
}